<?php


namespace app\model;


use think\facade\Cache;
use think\facade\Db;
use think\Model;

class Collect extends Model
{

    public $redis;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->redis = Cache::store('redis');
    }

    /**
     * 收藏
     * @param $data
     * @return mixed
     */
    public function Collect($data)
    {
        RedisLock('CollectLock_' . $data->user_id);
//        Db::startTrans();
        try {
            $key = 'Collect_' . $data->user_id;
            $goods_id = $data->data['goods_id'];
            $goods_id = explode(',',$goods_id);
            if(!empty($goods_id)){
                foreach ($goods_id as $v){
                                $goodsKey = 'Collect_' . $v;
                                $collect = $this->redis->sismember($key, $v);
                                if (!$collect) {
                                    $this->redis->sadd($key, $v);
                                    $this->redis->sadd($goodsKey, $data->user_id);
                    //                $this->save(['user_id' => $data->user_id, 'goods_id' => $goods_id]);
                                } else {
                                    $this->redis->srem($key, $v);
                                    $this->redis->srem($goodsKey, $data->user_id);
                    //                $this->where(['user_id' => $data->user_id, 'goods_id' => $goods_id])->delete();
                                }
                }
                    return app('json')->json_success('成功');
            }
            return app('json')->json_error('商品不存在');
//            Db::commit();
        
        } catch (\Exception $e) {
//            Db::rollback();
            return app('json')->json_error('收藏失败');
        }
    }

    /**
     * 收藏列表
     * @param $data
     * @return mixed
     */
    public function CollectList($data)
    {
        $key = 'Collect_' . $data->user_id;
        $collect = $this->redis->smembers($key);
        if (!empty($collect)) {
            $map = $data->data;
            $map['sort'] = '';
            $map['collect'] = $collect;
            return app()->make(CommodityGoods::class)->GoodsList($map, 2);
        }
        return app('json')->json_success('成功', $collect);
    }

    /**
     * 购物车编辑
     * @param $data
     * @return mixed
     */
    public function CollectDel($data)
    {
        RedisLock('CollectDelLock_' . $data->user_id);
        $key = 'Collect_' . $data->user_id;
        $ids = explode(',', $data->data['ids']);
        if (!empty($ids)) {
            foreach ($ids as $v) {
                $this->redis->srem($key, $v);
                $goodsKey = 'Collect_' . $v;
                $this->redis->srem($goodsKey, $data->user_id);
            }
        }
        return app('json')->json_success('成功');
    }

    public function videoChoose()
    {
        $arrs = array(
            ['id' => 1, 'name' => '第三方！', 'rate' => 50],
            ['id' => 2, 'name' => '本地', 'rate' => 50],
        );
        foreach ($arrs as $key => $val) {
            $arr[$val['id']] = $val['rate'];
        }
        $rid = getRand($arr);
        $result['type'] = $arrs[$rid - 1]['id'];
        $result['list'] = [];
        if ($result['type'] == 2) {
            //寻找本地视频
            $list = Db::name('video')->select()->toArray();
            foreach ($list as $key => &$val) {
                $arr_video[$val['id']] = $val['rate'];
            }
            $id_video = getRand($arr_video);
            $s = $list[$id_video - 1];
            unset($s['created_time']);
            unset($s['rate']);
            unset($s['name']);
            $result['list'][] = $s;
        }
        return app('json')->json_success('成功', $result);
    }
}